import java.sql.*;
import java.util.Scanner;

class User {
    static float pay;

    public void welcome() {
        System.out.println("1.看车");
        System.out.println("2.租车");
        System.out.println("3.换车");

        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();
        if (choice == 1) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");
                Statement stm = conn.createStatement();
                ResultSet rs = stm.executeQuery("select * from car ");
                System.out.println("车牌号品牌\t品牌\t\t\t类型\t\t\t日租金\t\t座位或载重量");
                while (rs.next()) {
                    System.out.printf("%-12s", rs.getString(1));
                    System.out.printf("%-12s", rs.getString(2));
                    System.out.printf("%-12s", rs.getString(3));
                    System.out.printf("%-12s", rs.getInt(4));
                    System.out.printf("%-18s", rs.getString(5));
                    System.out.println();
                    //管理员操作入口
                    //
                    //
                }
                rs.close();
                stm.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } else if (choice == 2) {
            Factory factory = new Factory();
            Vehicle vehicle = factory.welcome();
            boolean flag = vehicle.leaseOutFlow();
            if (flag) {
                update(vehicle);
                newMoney();
            }
        }
        else
        {
            Factory factory = new Factory();
            Vehicle vehicle = factory.welcome();
            boolean flag = vehicle.leaseOutFlow();
            if (flag) {
                update(vehicle);
                newMoney();
                System.out.println("换车成功！");
            }
        }

    }

    public void newMoney()
    {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException q) {
            q.printStackTrace();
        }
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException q) {
            q.printStackTrace();
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");
            Statement stm = conn.createStatement();
            ResultSet rs = stm.executeQuery("select * from money ");
            float surplus=0;
            while(rs.next())
            {
                surplus = rs.getFloat(1);
            }
            rs.close();
            stm.close();

            PreparedStatement pstm = conn.prepareStatement("update money set sum = ?");
            pstm.setFloat(1,surplus+User.pay);
            pstm.executeUpdate();

            pstm.close();
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void update(Vehicle vehicle)
    {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException q) {
            q.printStackTrace();
        }
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException q) {
            q.printStackTrace();
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");

            PreparedStatement pstm = conn.prepareStatement("update user set Vehicle = ? where  id = ? and password = ?");
            pstm.setString(1,vehicle.getBrand()+"-"+vehicle.getType()+"-"+vehicle.getVehicleid());
            pstm.setString(2,RentCar.ID);
            pstm.setObject(3,RentCar.password);
            pstm.executeUpdate();
            pstm.close();
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void Rentcar()
    {
        System.out.println("1.轿车    2.客车    3.卡车");
        System.out.println("请选择汽车类型：");
        Scanner in= new Scanner(System.in);
        int choice;
        choice=in.nextInt();
    }
    public void Lookcar()
    {
        if(Vehicle.vehicle.size()==0)
        {
            System.out.println("没有可租车辆！");
        }
        else
        {
            System.out.println("车牌号\t\t租金");
            for(int i=0;i<Vehicle.vehicle.size()-1;i++)
            {
                Vehicle vehicles=(Vehicle) Vehicle.vehicle.get(i);
                System.out.println(vehicles.getVehicleid()+"\t\t"+vehicles.getPerRent());
            }
        }
    }
}
